package com.koron.css2.newProjectCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjSettlement;
import com.koron.css2.newProjectCenter.bean.query.PjSettlementQuery;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjSettlementMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Calendar;
import java.util.List;

/**
 * 工程报装-编制结算列表查询
 */
public class PjSettlemetList implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjSettlementQuery pjSettlementQuery = JsonUtils.objectToPojo(req.getData(),PjSettlementQuery.class);
            //传回来的时间没有时分秒，数据库里是有时分秒的，将结束当天包进来,结束时间设置成23:59:59
            if (pjSettlementQuery.getEndTime() != null){
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(pjSettlementQuery.getEndTime());
                calendar.set(Calendar.HOUR_OF_DAY,23);
                calendar.set(Calendar.MINUTE,59);
                calendar.set(Calendar.SECOND,59);
                pjSettlementQuery.setEndTime(calendar.getTime());
            }

            PjSettlementMapper pjSettlementMapper = factory.getMapper(PjSettlementMapper.class);
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);

            // 1. 分页
            PageHelper.startPage(pjSettlementQuery.getPage(), pjSettlementQuery.getPageCount());
            // 2. 查询数据
            List<PjSettlement> pjSettlementList = pjSettlementMapper.getList(pjSettlementQuery);
            for (PjSettlement pjSettlement:pjSettlementList) {
                PjInfoBean pjInfoBean = pjInfoMapper.selectById(pjSettlement.getConsultId());
                pjSettlement.setConsultBillNo(pjInfoBean.getBillNo());
                pjSettlement.setConsultName(pjInfoBean.getPjName());
            }
            // 3. 返回数据
            MessageBean<PageInfo> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"编制结算列表查询成功",PageInfo.class);
            messageBean.setData(new PageInfo<>(pjSettlementList));
            return messageBean;
        } catch (Exception e) {
            logger.error("编制结算列表查询失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"编制结算列表查询失败",null);
        }
    }
}
